﻿var marker = null;
var markerTitle = "";
var LocalLatLng;
var googleMap;
var markerLatLng = null;

var centerX = 41.110269851373786;
var centerY = 122.99810791015625;

var asMeishiMapClass = function (maptype, renderTarget, lnglatStr, icoPath) {

    this.maptype = maptype;
    this.map = null;
    this.mark = null;
    this.temMark = true;
    this.markLng;
    this.markLat;
    this.icoPath = icoPath;

    this.defaultLng = 122.99810791015625;
    this.defaultLat = 41.110269851373786;

    this.renderTarget = renderTarget;


    if (lnglatStr != "" && lnglatStr != undefined) {
        var lnglatArr = lnglatStr.split(",");
        this.markLng = lnglatArr[0];
        this.markLat = lnglatArr[1];
        this.temMark = false;
    }
    this.create();

}
asMeishiMapClass.getbetwwon = function (lat1, lng1, lat2, lng2) {
    var EARTH_RADIUS = 6378.137; //地球半径
    function rad(d) {
        return d * Math.PI / 180.0;
    }
    function getDistance(lat1, lng1, lat2, lng2) {
        var radLat1 = rad(lat1);
        var radLat2 = rad(lat2);
        var a = radLat1 - radLat2;
        var b = rad(lng1) - rad(lng2);
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000 * 1000) / 10000; //米
        return s;
    }
    return getDistance(lat1, lng1, lat2, lng2)
}


asMeishiMapClass.prototype = {
    create: function () {
        this.map = this.cBmap(this.renderTarget);
    },
    save: function () {
        var _markerLat = null;
        var _markerLng = null;

        if (this.mark != null) {
            
            _markerLng = this.mark.getPosition().lng;
            _markerLat = this.mark.getPosition().lat;
            this.temMark = false;
        } else {
            this.temMark = true;
        }
        var _this = this;
        $.post("/shop/Ajax_revise/" + getPageShopId(), { name: "longitudeAndLatitude", value: _markerLng + "," + _markerLat }, function (json) {
            var _offset = $("#"+_this.renderTarget).offset();
            actionTips.show("地图修改成功!", _offset.left, _offset.top)
            _this.markLng = _markerLng;
            _this.markLat = _markerLat;

        });

    },
    locDefault: function () {
        if (this.mark != null) {
            this.map.centerAndZoom(new BMap.Point(this.markLng, this.markLat), 14);
        } else {
            this.map.centerAndZoom(new BMap.Point(this.defaultLng, this.defaultLat), 13);
        }

    },
    clearMark: function () {
        this.map.clearOverlays();
        this.mark = null;
    },
    ableEdit: function () {
        if (this.mark == null) {

            this.cBmapMark(this.map, this.defaultLng, this.defaultLat);
        }
    },
    disableEdit: function () {
        if (this.temMark) {
            this.clearMark();
        }
    },
    //建立百度地图
    cBmap: function (renderTarget) {
        var mp = new BMap.Map(renderTarget);
        if (this.markLng) {
            mp.centerAndZoom(new BMap.Point(this.markLng, this.markLat), 14);
            this.cBmapMark(mp, this.markLng, this.markLat);
        } else {
            mp.centerAndZoom(new BMap.Point(this.defaultLng, this.defaultLat), 13);
        }
        mp.enableScrollWheelZoom();

        return mp;

    },
    //建立百度地图mark
    cBmapMark: function (_map, _lng, _lat) {

        var _icon = new BMap.Icon(this.icoPath + "images/googleFlag.png", new BMap.Size(38, 53));

        this.mark = new BMap.Marker(new BMap.Point(_lng, _lat), { icon: _icon });  // 创建标注
        this.markLng = _lng;
        this.markLat = _lat;

        _map.addOverlay(this.mark);
        this.mark.enableDragging();


    }
}







/*
function initialize(xy, mt) {

    if (xy != "" && xy != undefined) {
        xy = xy.split(",");
        markerLatLng = new google.maps.LatLng(xy[0], xy[1]);
        centerX = xy[0];
        centerY = xy[1];
        markerTitle = mt;
    } else {
        markerLatLng = null
        marker = null;
    }

    LocalLatLng = new google.maps.LatLng(centerX, centerY);
    var myOptions = { zoom: 13, center: LocalLatLng, mapTypeId: google.maps.MapTypeId.ROADMAP };
    googleMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var _markerLatLng;

    if (markerLatLng == null) {
        return;
    } else {

        _markerLatLng = LocalLatLng;
    }

    marker = new google.maps.Marker({

        draggable: true,
        position: _markerLatLng,
        map: googleMap,
        title: mt,
        //icon:"/Content/images/googleFlag.png",
        shape: {
            type: 'poly'
            //coord:[4,4,29,4,29,29,22,29,17,35,16,35,10,29,4,29,4,4]
        }
    })

    //        var infoWindow = new  google.maps.infoWindow({
    //            center:"Some title",
    //            position:new google.maps.LatLng(54, 14)
    //            })
    //            infoWindow.open(map);
}


//保存坐标
function saveMap() {
    //markerLatLng = marker.position;

    var _markerLatLng = null;
    if (marker == null) {
        _markerLatLng == null;
    } else {
        _markerLatLng = marker.position.toUrlValue();
    }
    $.post("/shop/Ajax_revise/" + getPageShopId(), { name: "longitudeAndLatitude", value: _markerLatLng }, function (json) {
        alert(json);
    });
}

function showMapEdit(mt) {

    $("#saveMapBtn").show();
    $("#reMapBtn").show();

    var _markerLatLng;
    if (markerLatLng == null) {
        _markerLatLng = LocalLatLng;
    } else {
        _markerLatLng = markerLatLng;
    }

    if (markerLatLng == null) {
        alert(LocalLatLng)
        marker = new google.maps.Marker({

            draggable: true,
            position: LocalLatLng,
            map: googleMap,
            title: mt,
            //icon: "/Content/images/googleFlag.png",
            shape: {
                type: 'poly'
                //coord:[4,4,29,4,29,29,22,29,17,35,16,35,10,29,4,29,4,4]
            }
        })
    }

}
function hideMapEdit() {

    $("#saveMapBtn").hide();
    $("#reMapBtn").hide();


}
*/